package ngram_reader.viewer.query;

import stringFunctions.StringFunctions;
import stringFunctions.Stringify;

/**
 *
 * @author Shimu
 * @date 7-Jul-2013
 */
public class AllStatSearchQuery extends NoStatSearchQuery {

    private String query;
    private final Accumulator yearMatchVolumeAc;

    protected AllStatSearchQuery(String query, Match match,
            int startGram, int endGram, boolean matchCase) {
        super(query, match, startGram, endGram, matchCase);

        // Make sure the query String is not empty
        if (query.isEmpty()) {
            throw new IllegalArgumentException("Search word/phrase is empty!");
        }

        this.yearMatchVolumeAc = new Accumulator() {

            private StringBuilder b = new StringBuilder(100);

            {
                reset();
            }

            @Override
            public void reset() {
                b.setLength(0);
                b.append("year\tmatch count\tvolume count\n");
            }

            @Override
            public void accumulate(int year, long match, long volume) {
                b.append(year);
                b.append('\t');
                b.append(match);
                b.append('\t');
                b.append(volume);
                b.append("\n");
            }

            @Override
            public String getAccumulatedValue() {
                return b.toString();
            }
        };
    }

    @Override
    public boolean ignoreStatistics() {
        return false;
    }

    @Override
    public Accumulator getAccumulator() {
        return this.yearMatchVolumeAc;
    }

    @Override
    public String toString() {
        return StringFunctions.concat(
                "Query: ", query,
                " Gram: [", Stringify.intToString(startGram), ",", Stringify.intToString(endGram), "]",
                " Year: all, Match Count: all, Volume Count: all");
    }
}
